<template>
    <editClassScheduleView ref="editClassScheduleView" />
    <uploadScheduleView ref="uploadScheduleView" />
    <div class="app-table">
        <div class="app-body">
            <div class="app-search">
                <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="班级" prop="gradeId" style="width:200px">
                        <el-select clearable v-model="form.className" autocomplete="off" placeholder="请选择班级">
                            <el-option v-for="item in classList" :key="item.classId" :value="item.className"
                                :label="item.className"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit" @keyup.enter="searchEnter">搜索</el-button>
                        <el-button type="success" @click="getScheduleTemplate">下载模板</el-button>
                        <el-button type="warning" @click="uploadSchedule">上传课表</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table size="small" :data="list" border @cell-click="changeInfo">
                <el-table-column :label="headerTitle" align="center">
                    <el-table-column prop="courseNumber" label="节次" width="75" align="center" />
                    <el-table-column label="星期一" align="center">
                        <el-table-column prop="monday.aliasName" label="科目" width="75" align="center" />
                        <el-table-column prop="monday.teacherName" label="人员" width="75" align="center" />
                    </el-table-column>
                    <el-table-column label="星期二" align="center">
                        <el-table-column prop="tuesday.aliasName" label="科目" width="75" align="center" />
                        <el-table-column prop="tuesday.teacherName" label="人员" width="75" align="center" />
                    </el-table-column>
                    <el-table-column label="星期三" align="center">
                        <el-table-column prop="wednesday.aliasName" label="科目" width="75" align="center" />
                        <el-table-column prop="wednesday.teacherName" label="人员" width="75" align="center" />
                    </el-table-column>
                    <el-table-column label="星期四" align="center">
                        <el-table-column prop="thursday.aliasName" label="科目" width="75" align="center" />
                        <el-table-column prop="thursday.teacherName" label="人员" width="75" align="center" />
                    </el-table-column>
                    <el-table-column label="星期五" align="center">
                        <el-table-column prop="friday.aliasName" label="科目" width="75" align="center" />
                        <el-table-column prop="friday.teacherName" label="人员" width="75" align="center" />
                    </el-table-column>
                    <el-table-column label="星期六" align="center">
                        <el-table-column prop="saturday.aliasName" label="科目" width="75" align="center" />
                        <el-table-column prop="saturday.teacherName" label="人员" width="75" align="center" />
                    </el-table-column>
                    <el-table-column label="星期日" align="center">
                        <el-table-column prop="sunday.aliasName" label="科目" width="75" align="center" />
                        <el-table-column prop="sunday.teacherName" label="人员" width="75" align="center" />
                    </el-table-column>
                    <el-table-column label="操作" align="center">
                        <template style="display: block" #default="scope">
                            <el-button size="small" type="danger"
                                @click="deleteScheduleCourseNumber(scope.row.courseNumber)">删除节次
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table-column>

                <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <div style="margin-top: 10px">
                <el-config-provider :locale="locale">
                    <el-pagination :total="total" :page-size="limit" :current-page="page"
                        layout="total, prev, pager, next, jumper" @size-change="changePageSize"
                        @current-change="changeCurrentPage">
                    </el-pagination>
                </el-config-provider>
            </div>
        </div>
        <bottomView></bottomView>
    </div>
</template>
<script>
import editClassScheduleView from '../../components/schedule/editClassSchedule';
import uploadScheduleView from "../../components/schedule/uploadSchedule";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
    name: "teacherSubjectList",
    data() {
        return {
            type: 0,
            title: "",
            page: 1,
            total: 0,
            limit: 1,
            list: [],
            headerTitle: "",
            className: "",
            campus: "",
            classList: [],
            weekList: {
                monday: '星期一',
                tuesday: '星期二',
                wednesday: '星期三',
                thursday: '星期四',
                friday: '星期五',
                saturday: '星期六',
                sunday: '星期日'
            },
            form: {
                teacherName: "",
                className: "",
                classId: ""
            },
        };
    },
    components: {
        editClassScheduleView,
        uploadScheduleView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        bottomView,
        ElMessage, Upload
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getClassScheduleList();
        this.getClassList();
    },
    methods: {
        uploadSchedule() {
            this.$refs.uploadScheduleView.title = '上传班级课表';
            this.$refs.uploadScheduleView.dialogFormVisible = true
        },
        changeInfo(row, column, cell, event) {
            let property = column.property;
            let temp = property.split('.');
            let info = row[temp[0]];
            console.log(property, row[temp[0]]);
            if (property != 'courseNumber') {
                this.editClassSubject(temp[0], info['aliasName'], row.courseNumber)
            }
        },
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.page = 1;
                this.getClassScheduleList();
            }
        },
        editClassSubject(dateName, aliasName, courseNumber) {
            this.$refs.editClassScheduleView.title = '修改课表';
            this.$refs.editClassScheduleView.classScheduleForm.className = this.className;
            this.$refs.editClassScheduleView.classScheduleForm.aliasName = aliasName;
            this.$refs.editClassScheduleView.classScheduleForm.courseNumber = courseNumber;
            this.$refs.editClassScheduleView.classScheduleForm.campus = this.campus;
            this.$refs.editClassScheduleView.classScheduleForm.dateName = dateName;
            this.$refs.editClassScheduleView.classScheduleForm.week = this.weekList[dateName];
            this.$refs.editClassScheduleView.getAliasList();
            this.$refs.editClassScheduleView.dialogFormVisible = true
        },
        deleteScheduleCourseNumber(courseNumber) {
            ElMessageBox.confirm("你确定要删除该节课吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    let list = new FormData();
                    list.append('className', this.className);
                    list.append('courseNumber', courseNumber);
                    httpService("/pythonApi/scheduleApi/deleteCourseNumber",
                        list, "post").then(
                            (data) => {
                                if (data.code == 200) {
                                    ElMessage.success(data.message);
                                    this.getClassScheduleList();
                                } else {
                                    ElMessage.error(data.message);
                                }
                            }
                        );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getClassScheduleList()
        },
        getScheduleTemplate() {
            let config = { "responseType": "blob" };
            httpService(
                "/pythonApi/scheduleApi/getScheduleTemplate",
                {
                    fileType: 'schedule'
                },
                "get", config
            ).then((data) => {
                let link = document.createElement('a'), fileUrl = window.URL.createObjectURL(new Blob([data]));
                link.style.display = 'none';
                link.href = fileUrl;
                link.setAttribute('download', '班级课表模板.xlsx');
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
                window.URL.revokeObjectURL(fileUrl)
            });
        },
        onSubmit() {
            this.page = 1;
            this.getClassScheduleList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getClassScheduleList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getClassScheduleList();
        },
        getClassScheduleList() {
            httpService(
                "/pythonApi/scheduleApi/getClassScheduleList",
                {
                    page: this.page,
                    limit: this.limit,
                    className: this.form.className ? this.form.className : ""
                },
                "get"
            ).then((data) => {
                this.list = data.data;
                this.total = data.total;
                this.headerTitle = data.className + '课表';
                this.className = data.className;
                this.campus = data.campus
            });
        },
        getClassList() {
            httpService(
                "/pythonApi/scheduleApi/getClassList",
                {
                    classId: this.form.classId
                },
                "get"
            ).then((data) => {
                this.classList = data.data;
            });
        },
    },
};
</script>
<style scoped>
.app-table {
    background: #eff3f5;
    padding: 10px;
    width: calc(100vw - 190px);
}

.app-search {}


.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 243px);
}
</style>
